package fi.hell.pollit.tag.util

import fi.hell.pollit.domain.*;
import org.springframework.util.StringUtils
import org.springframework.web.servlet.support.RequestContextUtils as RCU

class UtilTagLib {
    static namespace = "p";

    def userLocale = { attrs ->
        User loggedInUser = User.getLoggedInUser();
        out << (loggedInUser ? loggedInUser.locale : resolveLocale());
    }

    def resolveLocale(localeAttr) {
        def locale = localeAttr
        if (locale != null && !(locale instanceof Locale)) {
            locale = StringUtils.parseLocaleString(locale as String)
        }
        if (locale == null) {
            locale = RCU.getLocale(request)
            if (locale == null) {
                locale = Locale.getDefault()
            }
        }
        return locale
    }
}
